import { reqGetCode ,reqUserRegister,  reqUserLogin,reqUserInfo} from "../api";
const state={
    getCode:'',
    userLogin:'',
    getUserInfo:{}
};
const mutations={
    GETCODE(state,getCode){
        state.getCode=getCode
    },
    USERLOGIN(state,userLogin){
      state.userLogin=userLogin
    },
    GETUSERINFO(state,getUserInfo){
      state.getUserInfo=getUserInfo
    },
};
const actions={
   async getCode({commit},phone){
     let {data}= await  reqGetCode();
     console.log(data,'验证码库');
     if(data.code==200){
        commit('GETCODE',data)
        return 'ok'
     }else{
         return Promise.reject(new Error('false'));
     }
    },
    async userRegister({commit},user) {
        let {data} = await reqUserRegister(user);
        console.log(data,'注册库');
        if (data.code == 200) {
          return "ok";
        } else {
          return Promise.reject(new Error("faile"));
        }
      },
      async userLogin({ commit }, data) {
        let result = await reqUserLogin(data);
        // console.log(result)
        if(result.data.code==200){
          // console.log(result.data.data.token)
          commit('USERLOGIN',result.data.data.token)
          return 'ok'
        }
        else {
            return Promise.reject(new Error("faile"));
          }
        //服务器下发token，用户唯一标识符(uuid)
        //将来经常通过带token找服务器要用户信息进行展示
        // if (result.code == 200) {
        //   //用户已经登录成功且获取到token
        //   commit("USERLOGIN", result.data.token);
        //   //持久化存储token
        //   setToken(result.data.token);
        //   return "ok";
        // } else {
        //   return Promise.reject(new Error("faile"));
        // }
      },
      async getUserInfo({ commit }) {
        let result = await reqUserInfo();
        // console.log(result)
        if (result.data.code == 200) {
          //提交用户信息
          commit("GETUSERINFO", result.data.data);
          return 'ok';
        }else{
          return Promise.reject(new Error('faile'));
        }
      },
    
};
const getters={};

export default{
    state,
    mutations,
    actions,
    getters

}